<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>砍价商品列表</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="/static/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="/static/layuiadmin/style/admin.css" media="all">
</head>
<body>

<style>
    .layui-table-cell{  /*最后的pic为字段的field*/
        height: 100%;
        max-width: 100%;
    }
</style>

<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-header">砍价商品列表</div>
                <div class="layui-card-body">


                    <div class="layui-form-item layui-form">
                        <div class="layui-btn-group test-table-operate-btn">
                            <button class="layui-btn" data-type="addadmin">添加砍价商品</button>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">商品名称</label>
                            <div class="layui-input-block">
                                <input type="text" name="goods_name" id="shop_name" placeholder="请输入" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <div class="layui-btn-group test-table-operate-btn">
                                <button class="layui-btn" data-type="searchUserButton">
                                    <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                                </button>
                            </div>
                        </div>
                    </div>

                    <table class="layui-hide" id="test-table-operate" lay-filter="test-table-operate" lay-size="lg">

                        <script type="text/html" id="is_use_switch">
                            <input type="checkbox" name="is_display" lay-skin="switch" lay-text="上架|下架" lay-filter="is_use_switch" value="{{ d.is_display }}" data-json="{{ encodeURIComponent(JSON.stringify(d)) }}" {{ d.is_display == 1 ? 'checked' : '' }}>
                        </script>

                    </table>

                    <script type="text/html" id="test-table-operate-barDemo">

                        <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="public">商品详情</a>
                        <a class="layui-btn layui-btn-normal layui-btn-xs" lay-event="edit">修改</a>
                        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
                    </script>
                </div>
            </div>
        </div>
    </div>
</div>

<!--修改密码-->
<div id="editpassmodal"  style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">
        <div class="layui-form-item">
            <label class="layui-form-label">最新开奖码</label>
            <div class="layui-input-block">
                <input type="text" name="content"  id="editMinganContent" class="layui-input">
                <input type="hidden" name="id"  id="editMianganid">
            </div>
        </div>


        <div class="layui-form-item layui-layout-admin">
            <div class="layui-input-block">
                <div class="layui-inline" style="left: 0;">
                    <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="editmingansubmit">立即提交</button>
                </div>
            </div>
        </div>
    </form>
</div>


<!--修改密码-->
<div id="passmodal"  style="display: none;padding:20px;">
    <form class="layui-form" action="" lay-filter="component-form-group">
        <div class="layui-form-item">

            <p id="show" style="text-align: center;font-size: 20px;color:red" ></p>
        </div>
        <input type="hidden" name="id" id="id">
        <div style="display: flex;justify-content: center">
            <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="submits">确定</button>
        </div>


    </form>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index','form','table','laydate','set'], function(){
        $ = layui.jquery;
        var table = layui.table
            ,admin = layui.admin
            ,element = layui.element
            ,layer = layui.layer
            ,laydate = layui.laydate
            ,form = layui.form;

        table.render({
            elem: '#test-table-operate'
            ,url: "{:url('Bargain/goods')}"
            ,height: 'full-155'
            ,cols: [[
                {field:'id',width:80,align:"center",  title: 'ID'}
                ,{field:'goods_image', align:"center",  title: '商品封面',style:"height:60px;",templet:function(d){
                        return "<img  style='width:50px;height:50px;' src=" +d.goods_image+ ">"
                    }}
                ,{field:'goods_name',align:"center",title: '商品名称'}

                ,{field:'goods_money',align:"center",title: '商品价格'}
                ,{field:'goods_number',align:"center",title: '库存'}
                ,{field:'join_count',align:"center",title: '新人参与数'}
                ,{field:'bargain_money',align:"center",title: '砍到多少钱'}
                ,{field:'is_display',width:150, align:'center', title: '是否上架',templet: '#is_use_switch'}
                ,{align:'center',width:350, title: '操作',fixed: 'right', toolbar: '#test-table-operate-barDemo'}
            ]]
            ,page: true
            ,limit: 20
        });

        form.render(null, 'component-form-group');

        laydate.render({
            elem: '#LAY-component-form-group-date'
        });
        //监听是否上架
        form.on('switch(is_use_switch)', function(obj){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            var json = JSON.parse(decodeURIComponent($(this).data('json')));
            json = table.clearCacheKey(json);

            var is_display = this.checked ? '1' : '0';

            $.ajax({
                url: "{:url('Bargain/isShow')}",
                type: 'post',
                dataType: 'json',
                data: {id:json.id,is_display:is_display},
                success: function(info){
                    if (info.code === 0) {
                        layer.close(loading);
                    }else{
                        layer.msg(info.msg,{icon:2,time:1000},function(){
                            window.location.reload();
                        });
                    }
                }
            })
        });
        //监听工具条
        table.on('tool(test-table-operate)', function(obj){
            var data = obj.data;
            if(obj.event === 'edit'){
                window.location.href="{:url('Bargain/editGoods')}?id=" + data.id;

            } else if(obj.event === 'del'){
                layer.confirm('确认删除吗？', function(index){
                    var loading = layer.load(1, {
                        shade: [0.1,'#fff'] //0.1透明度的白色背景
                    });
                    $.ajax({
                        url: "{:url('Bargain/del')}",
                        type: 'post',
                        dataType: 'json',
                        data: {id:data.id},
                        success: function(msg){
                            layer.close(loading);
                            if (msg === "1") {
                                obj.del();
                                layer.close(index);
                            }else{
                                layer.msg('删除失败',{icon:2,time:1000});
                            }
                        }
                    })

                });
            }else if(obj.event === 'public'){
                window.open("/admin/bargain/details/id/"+data.id);
            }
        });

        //添加提交
        form.on('submit(addsubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Comment/add')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(data){
                    if (data.code === 0) {
                        layer.msg('添加成功',{icon:1,time:2000},function(){
                            window.location.reload();
                        });
                    }else{
                        layer.close(loading);
                        layer.msg(data.code,{icon:2,time:1000})
                    }
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
        //修改敏感字
        form.on('submit(editmingansubmit)', function(data){
            var loading = layer.load(1, {
                shade: [0.1,'#fff'] //0.1透明度的白色背景
            });
            $.ajax({
                url: "{:url('Jlou/kai')}",
                type: 'post',
                dataType: 'json',
                data: {data:data.field},
                success: function(info){
                    layer.close(loading);
                    layer.closeAll();
                    $('#show').html(info.code);
                    var editpassmodal = layer.open({
                        title:'查看中奖号码'
                        ,type: 1
                        ,shadeClose: true
                        ,area: admin.screen() < 2 ? ['60%'] : ['500px']
                        ,closeBtn :0
                        ,content: $('#passmodal')
                    });
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });


        //确认密码
        form.on('submit(submits)', function(data){
            layer.closeAll();
            window.location.reload();
        });
        var $ = layui.$, active = {
            searchUserButton:function(){
                var shop_name  = $('#shop_name').val();
                table.reload('test-table-operate', {
                    where: {
                        shop_name: shop_name
                    }
                    ,page: {
                        curr: 1 //重新从第 1 页开始
                    }
                });
                //return false;
            },
            addadmin: function() {
                window.location.href="{:url('Bargain/addGoods')}";
            }
        };
        //绑定点击事件
        $('.test-table-operate-btn .layui-btn').on('click', function(){
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });


    });



</script>


</body>
</html>